package com.can.core.advice;

import com.can.common.constant.JsonResultEnum;
import com.can.entityDTO.JsonResult;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description shiro权限控制异常捕获控制类
 * @Author can
 * @Date 2018/8/28
 * @Version 1.0
 */
@ControllerAdvice
public class ShiroControllerAdvice{


        /**
         * 用户权限验证
         * @param ex
         * @return
         */
        //这个注解是指当controller中抛出这个指定的异常类的时候，都会转到这个方法中来处理异常
        @ExceptionHandler(UnauthorizedException.class)
        @ResponseBody
        //返回的状态码
        public JsonResult handlerAuthenticationException(UnauthorizedException ex){
            return new JsonResult(JsonResultEnum.UNAUTHORIZED_ERROR);
        }


        /**
         * 用户登录验证
         * @param ex
         * @return
         */
        @ExceptionHandler(AuthenticationException.class)
        @ResponseBody
        //返回的状态码
        public JsonResult handlerUserException(AuthenticationException ex){
             return new JsonResult(JsonResultEnum.ACCOUNT_PASSWORD_ERROR);
        }
}
